package com.yanqu.road.dao.impl.activity.seacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaCrossUnionData;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/2/12 18:11
 */
public class SeacraftSouthSeaCrossUnionDataDaoImpl extends TempDataObjectDao<SeacraftSouthSeaCrossUnionData> {
    public static String getSeacraftSouthSeaCrossUnionDataSql = "select * from `t_c_activity_seacraft_southsea_union` where `activityId`=?";
    public Map<String, SeacraftSouthSeaCrossUnionData> getSeacraftSouthSeaCrossUnionDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, SeacraftSouthSeaCrossUnionData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaCrossUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaCrossUnionData item = parse(rs);
                map.put(item.getUnionId(),item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSeacraftSouthSeaCrossUnionDataSql = "update `t_c_activity_seacraft_southsea_union` set round=?,rank=?,resultMap=?,donationShipMap=?,memberSet=?,fixDurableMap=?,unionBaseInfo=? where activityId=? and unionId=?";
    @Override
    protected boolean updateData(SeacraftSouthSeaCrossUnionData seacraftSouthSeaCrossUnionData) {
        seacraftSouthSeaCrossUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getResultMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getDonationShipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getMemberSet())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getFixDurableMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossUnionData.getUnionId()));
        if (getDb().executeNoneQuery(updateSeacraftSouthSeaCrossUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftSouthSeaCrossUnionDataSql = "insert into `t_c_activity_seacraft_southsea_union`(activityId,unionId,round,rank,resultMap,donationShipMap,memberSet,fixDurableMap,unionBaseInfo) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SeacraftSouthSeaCrossUnionData seacraftSouthSeaCrossUnionData) {
        seacraftSouthSeaCrossUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, seacraftSouthSeaCrossUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftSouthSeaCrossUnionData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getResultMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getDonationShipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getMemberSet())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getFixDurableMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftSouthSeaCrossUnionData.getUnionBaseInfo())));
        if (getDb().executeNoneQuery(insertSeacraftSouthSeaCrossUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftSouthSeaCrossUnionData parseData(ResultSet rs) throws SQLException {
        SeacraftSouthSeaCrossUnionData seacraftSouthSeaCrossUnionData = new SeacraftSouthSeaCrossUnionData();
        seacraftSouthSeaCrossUnionData.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaCrossUnionData.setUnionId(rs.getString("unionId"));
        seacraftSouthSeaCrossUnionData.setRound(rs.getInt("round"));
        seacraftSouthSeaCrossUnionData.setRank(rs.getInt("rank"));
        seacraftSouthSeaCrossUnionData.setResultMap(JSON.parseObject(rs.getString("resultMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaResultInfo>>() {}));
        seacraftSouthSeaCrossUnionData.setDonationShipMap(JSON.parseObject(rs.getString("donationShipMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Long>>() {}));
        seacraftSouthSeaCrossUnionData.setMemberSet(JSON.parseObject(rs.getString("memberSet"), Set.class));
        seacraftSouthSeaCrossUnionData.setFixDurableMap(JSON.parseObject(rs.getString("fixDurableMap"), new TypeReference<java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.seacraft.SeacraftSouthSeaFixDurableInfo>>() {}));
        seacraftSouthSeaCrossUnionData.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));

        return seacraftSouthSeaCrossUnionData;
    }
}
